package Options::Attributes::API::InterfaceValidateWebOrder;

use Options::Attributes::API::WebServiceLink;
our @ISA = qw(Options::Attributes::API::WebServiceLink)
    ;    # inherits from WebServiceLink

sub new {
    my ($class) = @_;

    # Call the constructor of the parent class.
    my $self = $class->SUPER::new( $_[1], $_[2], $_[3], $_[4] );
    bless $self, $class;
    return $self;
}

sub _setupParams {
    my $self      = shift;
    my %dataTypes = (
        'CalculateShipping'         => 'boolean',
        'CardAmount'                => 'decimal',
        'CashAmount'                => 'decimal',
        'CatalogueRequest'          => 'boolean',
        'CatalogueRequestSourceKey' => 'string',
        'ChequeAmount'              => 'decimal',
        'CreditAmount'              => 'decimal',
        'CountryCode'               => 'string',
        'CustomerNumber'            => 'int',
        'Currency'                  => 'string',
        'CustomerType'              => 'string',
        'Despatched'                => 'boolean',
        'DeliverDate'               => 'dateTime',
        'Discount'                  => 'decimal',
        'OrderDate'                 => 'dateTime',
        'OrderNumber'               => 'string',
        'OrderType'                 => 'string',
        'PaidFor'                   => 'boolean',
        'PrimaryPaymentMethod'      => 'string',
        'SalesNumber'               => 'string',
        'Reprice'                   => 'boolean',
        'ShipComplete'              => 'boolean',
        'ShippingMethod'            => 'string',
        'ShippingCost'              => 'decimal',
        'ShopNumber'                => 'int',
        'SourceKey'                 => 'string',
        'ShopperID'                 => 'string',
        'TaxRefund'                 => 'boolean',
        'TillNumber'                => 'int',
        'VoucherAmount'             => 'decimal',
        'Warehouse'                 => 'string',
        'CCType'                    => 'string',
        'CCNumber'                  => 'string',
        'CCCv2'                     => 'string',
        'CCExpiryMonth'             => 'int',
        'CCExpiryYear'              => 'int',
        'CCIssueNumber'             => 'int',
        'CCStartMonth'              => 'int',
        'CCStartYear'               => 'int',
        'CCAuthCode'                => 'string',
        'CCTransactionCode'         => 'string',
        'CCReceiptNumber'           => 'string',
        'EnteredBy'                 => 'string',
        'ProductCode'               => 'string',
        'Quantity'                  => 'int',
        'Price'                     => 'decimal',
        'ManualPrice'               => 'boolean',
        'Description'               => 'string',
        'IsPromotion'               => 'boolean',
        'PricePer'                  => 'int',
        'SourceKey'                 => 'string',
        'CCChargeCode'              => 'string',
        'CCDescription'             => 'string',
        'CCUnitCharge'              => 'decimal',
        'GCChargeCode'              => 'string',
        'GCDescription'             => 'string',
        'GCUnitCharge'              => 'decimal',
        'TextType' => 'tns:TextTypeType',    # Internal - option - investigate
        'Comment'  => 'string',
    );

    my @CreditCard
        = qw(CCType CCNumber CCCv2 CCExpiryMonth CCExpiryYear CCIssueNumber CCStartMonth CCStartYear CCAuthCode CCTransactionCode CCReceiptNumber);
    my @OrderHeader
        = qw(CalculateShipping CardAmount CashAmount CatalogueRequest CatalogueRequestSourceKey ChequeAmount CreditAmount CountryCode CustomerNumber ShopperID Currency CustomerType Despatched DeliverDate Discount OrderDate OrderNumber OrderType PaidFor PrimaryPaymentMethod SalesNumber Reprice ShipComplete ShippingMethod ShippingCost SourceKey TaxRefund VoucherAmount Warehouse EnteredBy);
    my @OrderLine
        = qw(ProductCode Quantity Price ManualPrice Description IsPromotion PricePer SourceKey );
    my @CCOrderCharge        = qw(CCChargeCode CCDescription CCUnitCharge);
    my @GCOrderCharge        = qw(GCChargeCode GCDescription GCUnitCharge);
    my @OrderText            = qw(TextType Comment);
    my @SalesOrder           = qw(OrderHeader OrderLines );
    my @Comments             = qw(OrderText);
    my @GeneralCharges       = qw(OrderCharge);
    my @CustomisationCharges = qw(OrderCharge);

    my $envelopeElement;
    my @partOrderHeader   = ();
    my @partCreditCard    = ();
    my @partOrderLine     = ();
    my @partOrderLines    = ();
    my @partCCOrderCharge = ();
    my @partGCOrderCharge = ();
    my @partOrderCharges1 = ();
    my @partOrderCharges2 = ();
    my @partSalesOrder    = ();
    my @partOrderText     = ();
    my @partOrderTexts    = ();

    foreach (@CreditCard) {
        $envelopeElement = SOAP::Data->type( $dataTypes{$_} )
            ->name( $_ => $self->{_WSRequest}->{$_} );
        push( @partCreditCard, $envelopeElement );
    }

    foreach (@OrderHeader) {
        $envelopeElement = SOAP::Data->type( $dataTypes{$_} )
            ->name( $_ => $self->{_WSRequest}->{$_} );
        push( @partOrderHeader, $envelopeElement );
    }

    #START DO THIS FOR EACH ORDER LINE
    foreach $OrderItem ( @{ $self->{_WSRequest}->{OrderLine} } ) {
        foreach (@OrderLine) {
            $envelopeElement = SOAP::Data->type( $dataTypes{$_} )
                ->name( $_ => $OrderItem->{$_} );
            push( @partOrderLine, $envelopeElement );
        }
        $envelopeElement = SOAP::Data->name(
            'OrderLine' => \SOAP::Data->value(@partOrderLine) );
        push( @partOrderLines, $envelopeElement );
        @partOrderLine = ();

        foreach (@CCOrderCharge) {
            $envelopeElement = SOAP::Data->type( $dataTypes{$_} )
                ->name( substr( $_, 2 ) => $OrderItem->{$_} );
            push( @partOrderCharge, $envelopeElement );
        }
        $envelopeElement = SOAP::Data->name(
            'OrderCharge' => \SOAP::Data->value(@partOrderCharge) );
        push( @partOrderCharges1, $envelopeElement );
        @partOrderCharge = ();

        foreach (@GCOrderCharge) {
            $envelopeElement = SOAP::Data->type( $dataTypes{$_} )
                ->name( substr( $_, 2 ) => $OrderItem->{$_} );
            push( @partOrderCharge, $envelopeElement );
        }
        $envelopeElement = SOAP::Data->name(
            'OrderCharge' => \SOAP::Data->value(@partOrderCharge) );
        push( @partOrderCharges2, $envelopeElement );
        @partOrderCharge = ();

        foreach (@OrderText) {
            $envelopeElement = SOAP::Data->type( $dataTypes{$_} )
                ->name( $_ => $OrderItem->{$_} );
            push( @partOrderText, $envelopeElement );
        }
        $envelopeElement = SOAP::Data->name(
            'OrderText' => \SOAP::Data->value(@partOrderText) );
        push( @partOrderTexts, $envelopeElement );
        @partOrderText = ();
    }

    #STOP DO THIS FOR EACH ORDER LINE

    #PUT ALL THE STUFF TOGETHER
    my @params = SOAP::Data->name(
        'SalesOrder' => \SOAP::Data->value(
            SOAP::Data->name(
                'OrderHeader' => \SOAP::Data->value(
                    @partOrderHeader,
                    SOAP::Data->name(
                        'CreditCard' => \SOAP::Data->value(@partCreditCard)
                    )
                )
            ),
            SOAP::Data->name(
                'OrderLines' => \SOAP::Data->value(@partOrderLines)
            ),
        )
    );
    return @params;
}

1;
